package com.hmall.order.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDTO;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OId;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private IOrderService orderService;

   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   @PostMapping
   public OId createOrder(@RequestBody OrderDTO orderDTO){
      return orderService.createOrder(orderDTO);
   }

   @GetMapping("/orderDetail/{id}")
   public OrderDetail queryOrderDetailById(@PathVariable Long id){
      return orderService.selectOrderDetailById(id);
   }

   @PutMapping("/update")
   public void update(@RequestBody Order order){
      LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(Order::getId, order.getId());
      orderService.update(order, wrapper);
   }

   @DeleteMapping("/{id}")
   public void deleteByOrderId(@PathVariable Long id) {
      orderService.deleteById(id);
   }
}
